//
//  XMIndexHeaderView.m
//  xiaorizi
//
//  Created by new-1020 on 2017/4/18.
//  Copyright © 2017年 begmoon. All rights reserved.
//

#import "XMIndexHeaderView.h"
#import "XMIndexCollectionViewCell.h"

#define  lineH 3
#define insetRight 10
#define insetLeft 20
@interface XMIndexHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSArray * titleArray;
@property (nonatomic,strong) UIView *lineView;
@property (nonatomic,assign) NSInteger currentINdex;
@end

@implementation XMIndexHeaderView

-(UIView *)lineView {
    if (_lineView == nil) {
        _lineView = [[UIView alloc]init];
    }
    return _lineView;
}

-(void)setHideLine:(BOOL)hideLine {
    _hideLine = hideLine;
    if (hideLine) {
        self.lineView.hidden = YES;
    }else {
        self.lineView.hidden = NO;
    }
}

-(instancetype)initWithFrame:(CGRect)frame WithTitleArray:(NSArray *)titleArray animation:(BOOL)animation {
    if (self = [super initWithFrame:frame]) {
        self.titleArray = titleArray;
        self.AnimationIsOpen = animation;
        [self creatUIWithTitleArray:titleArray];
    }
    return self;
}

-(void)creatUIWithTitleArray:(NSArray *)titleArray {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = [self GetSize];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    if (self.AnimationIsOpen == YES) {
        layout.sectionInset = UIEdgeInsetsMake(0, insetRight, 0, insetLeft);
        layout.minimumLineSpacing = 0;
    }else {
        layout.minimumLineSpacing = 0;
    }
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"XMIndexCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    
    self.lineView.frame = CGRectMake(0, self.size.height - lineH, 32, lineH);
    
    [self.collectionView addSubview:self.lineView];
    self.lineView.backgroundColor = XMColor(245, 70, 70);
    if (!self.AnimationIsOpen) {
        self.collectionView.bounces = NO;
        self.lineView.centerX = [self GetSize].width / 2;
    }else {
        self.lineView.centerX = [self GetSize].width / 2 + 10;
    }
}

-(CGSize)GetSize {
    if (self.titleArray.count < 5) {
        return CGSizeMake(self.frame.size.width / self.titleArray.count, self.frame.size.height - lineH - 1);
    }else {
        return CGSizeMake(self.frame.size.width / 5, self.frame.size.height - lineH -1);
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMIndexCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.titleLable.text = self.titleArray[indexPath.row];
    if (self.font) {
        cell.titleLable.font = self.font;
    }
    if (self.currentINdex == indexPath.row) {
        cell.titleLable.textColor = XMColor(245, 70, 70);
        if (self.selectFontColr) {
            cell.titleLable.textColor = self.selectFontColr;
        }
    }else {
        cell.titleLable.textColor = [UIColor blackColor];
        if (self.fontColor) {
            cell.titleLable.textColor = self.fontColor;
        }
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView cellForItemAtIndexPath:indexPath];
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.centerX = cell.center.x;
    }];
    if (self.AnimationIsOpen) {
         [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
    self.currentINdex = indexPath.row;
    [self.delegateXM XMIndexHeaderViewDidselectedWithIndex:indexPath.row];
    [self.collectionView reloadData];
}

-(void)LineViewScrolleToIndex:(NSInteger)index{
   
//    UICollectionViewCell * cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
   
    if (self.AnimationIsOpen) {
        [UIView animateWithDuration:0.2 animations:^{
            self.lineView.centerX = insetRight + [self GetSize].width / 2 + index * [self GetSize].width;
        }];
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }else {
        [UIView animateWithDuration:0.2 animations:^{
            self.lineView.centerX =  [self GetSize].width / 2 + index * [self GetSize].width;
        }];
    }
}

-(void)setLineColor:(UIColor *)lineColor {
    lineColor = lineColor;
    self.lineView.backgroundColor = lineColor;
}

-(void)setLineWidth:(CGFloat )lineWidth {
    _lineWidth = lineWidth;
    self.lineView.width = lineWidth;
    self.lineView.centerX = [self GetSize].width / 2;
}

-(void)setFont:(UIFont *)font {
    _font = font;
    [self.collectionView reloadData];
}

-(void)setFontColor:(UIColor *)fontColor {
    _fontColor = fontColor;
    [self.collectionView reloadData];
}

-(void)setSelectFontColr:(UIColor *)selectFontColr {
    _selectFontColr = selectFontColr;
    [self.collectionView reloadData];
}

@end
